package com.ml.mall.api.product;

import com.ml.mall.annotations.OperLog;
import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.BaseBeanReq;
import com.ml.mall.bean.req.product.*;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.consts.UserRoleTypeConsts;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.RetStatusNum;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.product.SysProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyyang
 * Created 2021-05-15
 */

@Api(tags = "【product】产品")
@RestController
@RequestMapping("/product")
public class SysProductController extends BaseController {

    @Autowired
    private SysProductService sysProductService;


    @RequestMapping(method = RequestMethod.POST, path = "/query_page")
    @ApiOperation("分页查询产品")
//    @RequiresPermissions(PermissionConst.PRODUCT_QUERY_PAGE)
    public ApiRestResult sysProductQueryPage(@RequestBody @Valid SysProductQueryPageReq req) {
        return ApiRestResult.ok(sysProductService.sysProductByPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/query_tree")
    @ApiOperation("查询产品树")
//    @RequiresPermissions(PermissionConst.PRODUCT_QUERY_TREE)
    public ApiRestResult sysProductQueryTree(@RequestBody @Valid SysProductQueryTreeReq req) {
        return ApiRestResult.ok(sysProductService.sysProductQueryTree(req));
    }


    //冗余商品搜索：目录ID 关键词
    @RequestMapping(method = RequestMethod.POST, path = "/shop_page")
    @ApiOperation("产品搜索")
//    @RequiresPermissions(PermissionConst.PRODUCT_SHOP_PAGE)
    public ApiRestResult sysProductQueryShopPage(@RequestBody @Valid SysProductShopPageReq sysProductShopPageReq) {
        return ApiRestResult.ok(sysProductService.sysProductQueryShopPage(sysProductShopPageReq));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/query_one")
    @ApiOperation("查询产品详情")
//    @RequiresPermissions(PermissionConst.PRODUCT_QUERY_ONE)
    public ApiRestResult sysProductQueryOne(@RequestBody @Valid BaseBeanReq req) {
        return ApiRestResult.ok(sysProductService.sysProductByOne(req));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/new")
    @OperLog(operDesc = "产品新增", operModul = "product", operType = "new")
    @ApiOperation("产品新增")
//    @RequiresPermissions(PermissionConst.PRODUCT_NEW)
    public ApiRestResult sysProductNew(@Valid @RequestBody SysProductNewReq req) {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(UserRoleTypeConsts.ROLE_TYPE_1!=currentUser.getRoleType()){
            req.setSupplierId(currentUser.getCompanyId());
        }
        return ApiRestResult.ok(sysProductService.sysProductNew(req));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/edit")
    @OperLog(operDesc = "产品编辑", operModul = "product", operType = "edit")
    @ApiOperation("产品编辑")
//    @RequiresPermissions(PermissionConst.PRODUCT_EDIT)
    public ApiRestResult sysProductEdit(@Valid @RequestBody SysProductEditReq req) {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(UserRoleTypeConsts.ROLE_TYPE_1!=currentUser.getRoleType()){
            req.setSupplierId(currentUser.getCompanyId());
        }
        sysProductService.sysProductEdit(req);
        return ApiRestResult.ok();
    }


    @RequestMapping(method = RequestMethod.DELETE, path = "/del")
    @OperLog(operDesc = "产品删除", operModul = "product", operType = "del")
    @ApiOperation("产品删除")
//    @RequiresPermissions(PermissionConst.PRODUCT_DEL)
    public ApiRestResult sysProductDel(@RequestParam("ids") List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            return ApiRestResult.err(RetStatusNum.PARAMS_ERROR.getMessage(), RetStatusNum.PARAMS_ERROR.getCode());
        }
        return ApiRestResult.ok(sysProductService.sysProductDel(ids));
    }
}
